/**
 * This file is part of aion-emu <aion-emu.com>.
 *
 *  aion-emu is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  aion-emu is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with aion-emu.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.aionengine.gameserver.network.aion.clientpackets;

import com.aionengine.gameserver.configs.administration.AdminConfig;
import com.aionengine.gameserver.configs.main.CustomConfig;
import com.aionengine.gameserver.configs.main.LoggingConfig;
import com.aionengine.gameserver.model.ChatType;
import com.aionengine.gameserver.model.gameobjects.player.FriendList;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.network.aion.AionClientPacket;
import com.aionengine.gameserver.network.aion.AionConnection.State;
import com.aionengine.gameserver.network.aion.serverpackets.SM_MESSAGE;
import com.aionengine.gameserver.network.aion.serverpackets.SM_SYSTEM_MESSAGE;
import com.aionengine.gameserver.restrictions.RestrictionsManager;
import com.aionengine.gameserver.services.MuiService;
import com.aionengine.gameserver.services.NameRestrictionService;
import com.aionengine.gameserver.utils.ChatUtil;
import com.aionengine.gameserver.utils.PacketSendUtility;
import com.aionengine.gameserver.utils.Util;
import com.aionengine.gameserver.world.World;
import com.aionengine.gameserver.services.FFAInstance;
import com.aionengine.gameserver.services.custom.FFAManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Packet that reads Whisper chat messages.<br>
 *
 * @author SoulKeeper
 */
public class CM_CHAT_MESSAGE_WHISPER extends AionClientPacket {

    /**
     * Logger
     */
    private static final Logger log = LoggerFactory.getLogger("CHAT_LOG");

    /**
     * To whom this message is sent
     */
    private String name;

    /**
     * Message text
     */
    private String message;

    /**
     * Constructs new client packet instance.
     *
     * @param opcode
     */
    public CM_CHAT_MESSAGE_WHISPER(int opcode, State state, State... restStates) {
        super(opcode, state, restStates);

    }

    /**
     * Read message
     */
    @Override
    protected void readImpl() {
        name = readS();
        message = readS();
    }

    /**
     * Print debug info
     */
    @Override
    protected void runImpl() {

        name = ChatUtil.getRealAdminName(name);

        String formatname = Util.convertName(name);

        Player sender = getConnection().getActivePlayer();
        Player receiver = World.getInstance().findPlayer(formatname);

        if (LoggingConfig.LOG_CHAT)
            log.info(String.format("[MESSAGE] [%s] Whisper To: %s, Message: %s", sender.getName(), formatname, message));

        if (FFAInstance.isInBG(sender) || FFAManager.isInFFA(sender) || FFAInstance.isInBG(receiver) || FFAManager.isInFFA(receiver)) {
            return;
        }
        
        if (receiver == null) {
			sendPacket(SM_SYSTEM_MESSAGE.STR_NO_SUCH_USER(formatname));
		}
        else if (receiver.getFriendList().getStatus() == FriendList.Status.OFFLINE && sender.getAccessLevel() < AdminConfig.GM_LEVEL)
        {
            sendPacket(SM_SYSTEM_MESSAGE.STR_NO_SUCH_USER(formatname));
        } else if (!receiver.isWispable()) {
            PacketSendUtility.sendMessage(sender, MuiService.getInstance().getMessage("WHISPER"));
            return;
        } else if (sender.getLevel() < CustomConfig.LEVEL_TO_WHISPER) {
            sendPacket(SM_SYSTEM_MESSAGE.STR_CANT_WHISPER_LEVEL(String.valueOf(CustomConfig.LEVEL_TO_WHISPER)));
        } else if (receiver.getBlockList().contains(sender.getObjectId())) {
            sendPacket(SM_SYSTEM_MESSAGE.STR_YOU_EXCLUDED(receiver.getName()));
        } else if ((!CustomConfig.SPEAKING_BETWEEN_FACTIONS)
                && (sender.getRace().getRaceId() != receiver.getRace().getRaceId())
                && (sender.getAccessLevel() < AdminConfig.GM_LEVEL) && (receiver.getAccessLevel() < AdminConfig.GM_LEVEL)) {
            sendPacket(SM_SYSTEM_MESSAGE.STR_NO_SUCH_USER(formatname));
        } else {
            if (RestrictionsManager.canChat(sender))
                PacketSendUtility.sendPacket(receiver, new SM_MESSAGE(sender, NameRestrictionService.filterMessage(message), ChatType.WHISPER));
        }
    }
}
